﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;

public partial class Pages_Admin_CustomerUserChange : BasePage
{
    private int _UserID = 0;
    protected int _CustomerID = 0;
    private bool _IsEditMode = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {            
            BindDropDownLists();
            BindUserInfo();
            ShowSuccessMessage();
        }
    }
    /// <summary>
    /// Bindis the Page Initialization Variables
    /// </summary>
    protected void BindPageInfo()
    {
        _UserID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        _CustomerID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.CUSTOMER_ID);

        if (_UserID > 0 && _CustomerID > 0)
        {
            _IsEditMode = true;
            h4Heading.InnerHtml = "Edit User";
        }
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }

    /// <summary>
    /// Binds Dropdownlists for the initial request.
    /// </summary>
    protected void BindDropDownLists()
    {
        BindDropdownList.Contacts(ddlContactID, _CustomerID);
        //BindDropdownList.Customers(ddlCustomerID);
        //BindDropdownList.UserGroups(ddlGroupID);

    }
    protected void ShowSuccessMessage()
    {
        if(String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.SUCCESS_MESSAGE), "True", true) == 0)
            WebUtil.ShowMessageBox(divMessage, "User Saved Successfully.", false);
    }
    /// <summary>
    /// Binds User Info Requested through Query Strings
    /// </summary>
    protected void BindUserInfo()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        if (_IsEditMode)
        {            
            User entity = context.Users.FirstOrDefault(P => P.ID == _UserID && P.CustomerID == _CustomerID);//dao.GetByID(_ID);
            if (entity == null)
                ShowNotFoundMessage();
            else
            {
                txtLoginName.Text = entity.LoginName;
                //ddlGroupID.SetSelectedItem(entity.GroupID.ToString());
                chkIsActive.Checked = entity.IsActive;
                //txtPassword.Text = entity.Password;
                //ddlCustomerID.SetSelectedItem(entity.CustomerID.ToString());
                ddlContactID.SetSelectedItem(entity.ContactID.ToString());
                rfvPassword.Enabled = false;
            }
        }
        else
        {
            chkIsActive.Checked = true;
            Customer customer = context.Customers.FirstOrDefault(P => P.ID == _CustomerID);
            if (customer == null)
                ShowNotFoundMessage();
        }
    }
    /// <summary>
    /// Shows a Message in the UI and Hides the Data Editing Controls
    /// </summary>
    protected void ShowNotFoundMessage()
    {
        pnlFormContainer.Visible = false;
        WebUtil.ShowMessageBox(divMessage, "Requested User was not found.", true);
    }
    protected void SaveUser()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        Customer customer = context.Customers.FirstOrDefault(P => P.ID == _CustomerID);
        User entity = null;

        if (_IsEditMode)
        {
            entity = context.Users.FirstOrDefault(P => P.ID == _UserID); //dao.GetByID(_ID);        
            if (!txtPassword.Text.Trim().IsNullOrEmpty())
                entity.Password = txtPassword.Text;
        }
        else
        {
            entity = new User();
            entity.CustomerID = _CustomerID;
            entity.Password = txtPassword.Text;            
            entity.GroupID = customer.BusinessTypeID == ConfigReader.DeveloperBusinessTypeID ? ConfigReader.DeveloperUserGroupID : ConfigReader.ServiceProviderUserGroupID;
            //entity.GroupID = ConfigReader.DeveloperUserGroupID;
            entity.Created = DateTime.Now;
            context.AddToUsers(entity);
        }
        entity.LoginName = txtLoginName.Text;        
        entity.IsActive = chkIsActive.Checked;                
        entity.Modified = DateTime.Now;
        entity.IsDeleted = false;
        entity.ContactID = ddlContactID.SelectedValue.ToInt();
        context.SaveChanges();
        String url = String.Format("{0}?{1}={2}&{3}=True", Request.Url.AbsolutePath, AppConstants.QueryString.CUSTOMER_ID, _CustomerID, AppConstants.QueryString.SUCCESS_MESSAGE);
        Response.Redirect(url);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SaveUser();
            //Response.Redirect(AppConstants.Pages.USER_LIST);
            return;
        }
    }
    protected void btnList_Click(object sender, EventArgs e)
    {
        //Response.Redirect(AppConstants.Pages.USER_LIST);
        //return;
    }
    protected void cvLoginName_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        BtcHousingEntities context = new BtcHousingEntities();

        if (_IsEditMode)
        {
            User user = context.Users.FirstOrDefault(P => String.Compare(P.LoginName, e.Value, true) == 0 && P.ID != _UserID);
            if (user == null)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        else
        {
            User user = context.Users.FirstOrDefault(P => String.Compare(P.LoginName, e.Value, true) == 0);
            if (user == null)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
    }
}